//
//  UpdateInfoViewController.m
//  CarTown
//
//  Created by xionghao on 15/8/7.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "UpdateInfoViewController.h"
#import "MyInfoBasicMode.h"
#import "ZilaioTableViewCell.h"
#import "JianjieViewController.h"
#import "ZhengjiaUpadteViewController.h"
#import "Puser.h"
#import "Address.h"
#import "CityPickerView.h"
#import "UserInfoUpdateResde.h"
#import "ComUpdateZJViewController.h"
@interface UpdateInfoViewController ()<UITableViewDataSource,UITableViewDelegate,CityPickerDelegate>
@property (nonatomic,weak) UITableView *tablview;
@property (nonatomic,strong) NSArray *array;
@property (nonatomic,assign) NSInteger count;
@property (nonatomic,strong) Puser *user;
@property (nonatomic,strong) CityPickerView *pickerView;
@property (nonatomic,copy) NSString *pro;
@property (nonatomic,copy) NSString *city;
@property (nonatomic,copy) NSString *dist;
@property (nonatomic,strong) MyInfoBasicMode *dizhi;
@property (nonatomic,strong) MyInfoBasicMode *xiangxi;
@end

@implementation UpdateInfoViewController
-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}
-(CityPickerView *)pickerView{
    if (_pickerView==nil) {
        _pickerView=[[CityPickerView alloc]initWithDelegate:self WithColoum:3];
    }
    return _pickerView;
}
-(void)citypicker:(NSString *)city{
    self.dizhi.detail=city;
    [self.tablview reloadData];
}
-(void)citypicker:(NSString *)procestr city:(NSString *)citystr dist:(NSString *)dist{
    self.pro=procestr;
    self.city=citystr;
    self.dist=dist;
}
- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationItem.title=@"修改认证信息";
    
    if ([self.user.power integerValue]<5) {
        [self setupdata];
    }else{
        [self setupComdata];
    }
    [self setuptablView];
    
    // Do any additional setup after loading the view from its nib.
}
-(void)getupdateInfo{
    
}

-(void)dealloc{
    [self.pickerView removeFromSuperview];
}

-(void)setuptablView{
    UITableView *tablview=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, XHScreenwidth, XHScreenheight) style:UITableViewStyleGrouped];
    self.tablview=tablview;
    tablview.delegate=self;
    tablview.dataSource=self;
    
    [self.view addSubview:tablview];
    
    UIView *footview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, XHScreenwidth, 40)];
    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(16,0, XHScreenwidth-32, 36)];
    [button setBackgroundImage:[UIImage imageWithStretchableName:@"button_f"] forState:UIControlStateNormal];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(updateinfo) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [footview addSubview:button];
    self.tablview.tableFooterView=footview;
    self.tablview.backgroundColor=bgGarycolor;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 16;
}
-(void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section{
    view.backgroundColor=bgGarycolor;
}

-(void)saveInfo{
    if ([self.user.power integerValue]<5) {
        self.user.user_address=self.xiangxi.detail;
        self.user.user_province=self.pro;
        self.user.user_city=self.city;
        self.user.user_district=self.dist;
    }else{
        self.user.com_adress=self.xiangxi.detail;
        self.user.shop_province=self.pro;
        self.user.shop_city=self.city;
        self.user.shop_district=self.dist;
    }
   
    [Puser save];
}

-(void)updateinfo{
    NSLog(@"提交");
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
   
    hud.margin = 10.f;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:1];
    if (![self.xiangxi.detail isEqualToString: self.user.user_address]||![self.dist isEqualToString: self.user.user_district]||self.dist==nil) {
        hud.labelText=@"正在更新";
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        if ([self.user.power integerValue]<5) {
         params[@"action"]=@"p_update_change";
            params[@"user_type"]=@"person";
            params[@"user_id"]=self.user.user_id;
            params[@"user_address"]=self.xiangxi.detail;
             params[@"area"]=self.dist;
        }else{
            params[@"action"]=@"c_pcd_change";
            params[@"user_type"]=@"company";
            params[@"com_id"]=self.user.com_id;
            params[@"district"]=self.dist;
        }
        
        params[@"province"]=self.pro;
        params[@"city"]=self.city;
        
        
        
        
        
        [PostUitls Post:userinfourl parameters:params success:^(id response) {
            hud.mode=MBProgressHUDModeText;
            UserInfoUpdateResde *mode=[UserInfoUpdateResde resdeWithDict:response];
            if ([mode.state integerValue]==400) {
                hud.labelText=@"更新成功";
                [self saveInfo];
            }else{
                hud.labelText=mode.msg;
            }
            [hud hide:YES afterDelay:1];
        } failure:^(NSError *error) {
            hud.mode=MBProgressHUDModeText;
            hud.labelText=@"网络异常";
             [hud hide:YES afterDelay:1];
        }
         ];
    }else{
        hud.mode=MBProgressHUDModeText;
        hud.labelText=@"数据没有更改，无需提交";
        [hud hide:YES afterDelay:1];
    }
}

-(void)setupComdata{
    self.pro=self.user.user_province;
    self.dist=self.user.user_district;
    self.city=self.user.user_city;
    MyInfoBasicMode *name=[[MyInfoBasicMode alloc]init];
    name.name=@"公司名称";
    name.detail=self.user.com_name;
    name.HideAccestory=YES;
    MyInfoBasicMode *zhengjian=[[MyInfoBasicMode alloc]init];
    zhengjian.name=@"营业执照编号";
    zhengjian.detail=self.user.com_zhizhao;
    zhengjian.HideAccestory=YES;
    
    MyInfoBasicMode *dizhi=[[MyInfoBasicMode alloc]init];
    dizhi.name=@"所在地";
    Address *pro=[Address getAdressbyID:self.user.shop_province];
    Address *city=[Address getAdressbyID:self.user.shop_city];
    Address *dist=[Address getAdressbyID:self.user.shop_district];
    
    dizhi.detail=[NSString stringWithFormat:@"%@%@市%@",pro.area_name,city.area_name,dist.area_name];
    dizhi.block=^{
        [self.pickerView showinView:self.view.superview];
    };
    self.dizhi=dizhi;
    MyInfoBasicMode *xiangxi=[[MyInfoBasicMode alloc]init];
    xiangxi.name=@"详细地址";
    if (![self.user.com_adress isEqualToString:@""]) {
        xiangxi.detail=self.user.com_adress;
    }else{
        xiangxi.detail=@"未填写";
    }
    
    xiangxi.block=^{
        JianjieViewController *jjvc=[JianjieViewController viewcontroller];
        jjvc.navtitle=@"详细地址";
        jjvc.text=[xiangxi.detail isEqualToString:@"未填写"]?@"":xiangxi.detail;
        jjvc.mode=self.user;
        jjvc.block=^(NSString *str){
            self.xiangxi.detail=str;
            [self.tablview reloadData];
        };
        [self.navigationController pushViewController: jjvc animated:YES];
        
    };
    self.xiangxi=xiangxi;
    MyInfoBasicMode *photo=[[MyInfoBasicMode alloc]init];
    photo.name=@"上传公司证件";
    photo.detail=@"已上传";
    photo.block=^{
        ComUpdateZJViewController *zjvc=[ComUpdateZJViewController Viewcontroller];
        [self.navigationController pushViewController:zjvc animated:YES];
    };
    self.array=@[@[name,zhengjian],@[dizhi,xiangxi,photo]];
    self.count=7;
}

-(void)setupdata{
    self.pro=self.user.user_province;
    self.dist=self.user.user_district;
    self.city=self.user.user_city;
    MyInfoBasicMode *name=[[MyInfoBasicMode alloc]init];
    name.name=@"姓名";
    name.detail=self.user.user_truename;
    name.HideAccestory=YES;
    MyInfoBasicMode *zhengjian=[[MyInfoBasicMode alloc]init];
    zhengjian.name=@"证件类型";
    zhengjian.detail=@"身份证";
    zhengjian.HideAccestory=YES;
    
    MyInfoBasicMode *zhenjiannumber=[[MyInfoBasicMode alloc]init];
    zhenjiannumber.name=@"证件号码";
    
    NSString *number=self.user.user_report;
    if ([self.user.user_report isEqualToString:@""]||self.user.user_report==nil) {
        
    }else{
    number=[number stringByReplacingCharactersInRange:NSMakeRange(number.length-10, 4) withString:@"****"];
    }
    zhenjiannumber.detail=number;
    zhenjiannumber.HideAccestory=YES;
    MyInfoBasicMode *dizhi=[[MyInfoBasicMode alloc]init];
    dizhi.name=@"所在地";
    if ([self.user.user_porttype integerValue]!=0) {
        Address *pro=[Address getAdressbyID:self.user.user_province];
        Address *city=[Address getAdressbyID:self.user.user_city];
        Address *dist=[Address getAdressbyID:self.user.user_district];
        
        dizhi.detail=[NSString stringWithFormat:@"%@%@市%@",pro.area_name,city.area_name,dist.area_name];
    }else{
        dizhi.detail=@"未填写";
    }

    dizhi.block=^{
        [self.pickerView showinView:self.view.superview];
    };
    self.dizhi=dizhi;
    MyInfoBasicMode *xiangxi=[[MyInfoBasicMode alloc]init];
    xiangxi.name=@"详细地址";
    if (![self.user.user_address isEqualToString:@""]) {
        xiangxi.detail=self.user.user_address;
    }else{
        xiangxi.detail=@"未填写";
    }
    
    xiangxi.block=^{
        JianjieViewController *jjvc=[XHMianStory instantiateViewControllerWithIdentifier:@"jianjie"];
        jjvc.navtitle=@"详细地址";
        jjvc.text=[xiangxi.detail isEqualToString:@"未填写"]?@"":xiangxi.detail;
        jjvc.block=^(NSString *str){
             self.xiangxi.detail=str;
            [self.tablview reloadData];
        };
        [self.navigationController pushViewController: jjvc animated:YES];

    };
    self.xiangxi=xiangxi;
    MyInfoBasicMode *photo=[[MyInfoBasicMode alloc]init];
    photo.name=@"上传手持证件照片";
    if ([self.user.power integerValue]!=4) {
         photo.detail=@"已上传";
    }else{
        photo.detail=@"未上传";
    }
    photo.block=^{
        ZhengjiaUpadteViewController *zjvc=[XHMianStory instantiateViewControllerWithIdentifier:@"zhengjian"];
        [self.navigationController pushViewController:zjvc animated:YES];
    };
    self.array=@[@[name,zhengjian,zhenjiannumber],@[dizhi,xiangxi,photo]];
    self.count=7;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array=self.array[indexPath.section];
    MyInfoBasicMode *mode=array[indexPath.row];
    if (mode.block) {
        mode.block();
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.array.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array=self.array[section];
    return array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     NSArray *array=self.array[indexPath.section];
    MyInfoBasicMode *mode=array[indexPath.row];
    ZilaioTableViewCell *cell=[ZilaioTableViewCell cellWithTablView:tableView];
    if (mode.HideAccestory) {
         cell.accessoryType=UITableViewCellAccessoryNone;
    }else{
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
   
    cell.textLabel.text=mode.name;
    cell.detailTextLabel.text=mode.detail;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
